/******************* 公共变量 *******************/
var urlData = core.getUrlData(['site', 'lng', 'lat', 'tel']),
	shopName = decodeURIComponent(urlData[0]),
	lng = urlData[1],
	lat = urlData[2];

/******************* 页面初始化代码 *******************/
$('#map').height(winHeight - $('html body .wrap .activity-map .position-add').height() - headerTitleHeight);

/******************* 初始化微信 *******************/
wx.ready(function () {
	wx.checkJsApi({  
		jsApiList : ['getLocation', 'uploadImage','previewImage','chooseImage','scanQRCode','startRecord','stopRecord','onVoiceRecordEnd','playVoice','onVoicePlayEnd','onMenuShareAppMessage'],  
		success : function(res) {  
		},
		complete: function (data) {
			if (data.checkResult.getLocation == 'no') {
				useGps(true);
			} else {
				useGps();
			}
		}
	});
});

function useGps(useBaidu) {
	/******************* 获取当前位置的IP地址 *******************/
	var bdMap = core.bdMap({
		selector: 'map',
		device: 'mb',
		map: true,
		setMap: function (map, x, y) {
			console.log(x, y);
			console.log(lng, lat);
			bdMap.gpsDetail(x, y, function (data) {
				var point = new BMap.Point(lng, lat);		// 使用店铺的百度地图经纬度创建地图定位点
				map.centerAndZoom(new BMap.Point(point), 14);		// 创建中心点为店铺位置
				map.addOverlay(new BMap.Marker(point));		// 添加店铺位置

				map.addOverlay(new BMap.Marker(new BMap.Point(data.result.location.lng, data.result.location.lat),{icon:new BMap.Icon('', new BMap.Size(25,25))}));

				map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
				map.setCenter(point);
				map.addControl(new BMap.NavigationControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}));

				console.log($('#map div div div:nth-child(2) .BMap_Marker'))

				$('#map').append('<div id="goBack" style="height: 32px; position: absolute; z-index: 1200; text-size-adjust: none; bottom: 30px; right: auto; top: auto; left: 10px;"><div style="height: 32px; margin: 0px; box-sizing: border-box; border: 1px solid #d9d7d5; border-radius: 3px; -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,.2); overflow: hidden;"><div style="float: left; width: 32px; height: 32px; background-image: url(http://api0.map.bdimg.com/images/geolocation-control/mobile/gradient-bg-1x64.png); background-size: 1px 32px; background-repeat: repeat-x;"><div style="width: 32px; height: 32px; cursor: pointer; background-image: url(http://api0.map.bdimg.com/images/geolocation-control/mobile/default-40x40.png); background-size: 20px 20px; background-repeat: no-repeat; background-position: center center;"></div></div></div></div>');
				$('#goBack').on('tap', function () {
					bdMap.map.setZoom(14);
					bdMap.map.setCenter(new BMap.Point(data.result.location.lng, data.result.location.lat));
				});
				$('body .wrap .position-add p').html('活动地点：' + decodeURIComponent(urlData[0]));
			})
		}
	}, useBaidu);
}


// bdMap.createMap(urlData[1], urlData[2]);
// bdMap.gpsDetail(urlData[1], urlData[2], function (data) {
// 	// console.log(data);
// 	$('body .wrap .position-add p').html('当前地址：' + data.result.formatted_address);
// });